import 'package:flutter/material.dart';
import 'package:flutter_wan_android/arch/net/http_manager.dart';
import 'package:flutter_wan_android/arch/net/result_data.dart';
import 'package:flutter_wan_android/arch/util/sputil.dart';
import 'package:flutter_wan_android/common/api.dart';
import 'package:flutter_wan_android/model/user_info.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/routes/route_util.dart';
import 'package:flutter_wan_android/routes/routes.dart';

import '../application.dart';
import '../r.dart';

class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {

  @override
  void initState() {
    Future.delayed(Duration(seconds: 3), () {
      RouteUtil.push(context, Routes.index, replace: true);
    });

    _autoLogin();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: WColor.blue1,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(R.imagesIcLogoWhite, width: 70, height: 70,),
            Padding(padding: EdgeInsets.only(top: 20)),
            Text('WanAndroid',style: TextStyle(fontSize: 18, color: WColor.white),),
            Container(height: 80,),
          ],
        ),
      ),
    );
  }


  /// 自动登录
  _autoLogin() async {

    String _user = await SpUtil.getString("userName");

    String _pwd = await SpUtil.getString("pwd");

    ResultData result = await HttpManager.instance().post(
      Api.LOGIN,
      params: {"username": _user, "password": _pwd},
      withLoading: false
    );

    // TODO
    RouteUtil.push(context, Routes.index, replace: true);
    Application.hasLogin = result.success;
    if (result.success) {
      Application.userInfo = UserInfo.fromJson(result.data);
    }
  }
}
